import ACTION from '../constant/action-type';

const Commands = {
  setSelection: (editor, selection) => {
    editor.applyAction(ACTION.SET_SELECTION, {
      selection,
    });
  },
  insertNodeByPath: (editor, node, path) => {
    editor.applyAction(ACTION.INSERT_NODE, {
      node,
      path,
    });
  },
  insertNodeByKey(editor, node, key, index = 0) {
    const { document } = editor.value;
    const path = document.getPath(key);
    editor.insertNodeByPath(node, path.concat(index));
  },
  removeNode(editor, key) {
    const { document } = editor.value;
    const path = document.getPath(key);
    const node = document.getNode(key);
    editor.applyAction(ACTION.REMOVE_NODE, {
      path,
      node,
    });
  },
  moveNode(editor, source, target, index = 0) {
    const { document } = editor.value;
    const targetPath = document.resolvePath(target, index);
    editor.moveNodeByPath(source, targetPath);
  },
  moveNodeByPath(editor, source, target) {
    editor.applyAction(ACTION.MOVE_NODE, {
      source,
      target,
    });
  },
  updateNode(editor, node, properties) {
    editor.applyAction(ACTION.UPDATE_NODE, {
      node,
      properties,
    });
  },
};

export default Commands;
